package com.usian.behavior.kafka;

import com.usian.behavior.service.ApFollowBehaviorService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static com.usian.common.constants.behavior.FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC;

@Component
public class FollowBehaviorListener {

    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = FOLLOW_BEHAVIOR_TOPIC)
    public void receiverMessage(ConsumerRecord<String,String> record){
        //接收关注行为数据，保存
        Optional<? extends ConsumerRecord<String,String>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            String value = record.value();
            String [] arr = value.split(",");
            apFollowBehaviorService.saveFollowBehavior(Integer.parseInt(arr[0]),Integer.valueOf(arr[1]),Long.parseLong(arr[2]));
        }
    }
}
